<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/27
 * Time: 17:22
 */

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
class RecordTransaction extends Model
{
    /**
     * 交易记录表
     * @var string
     */
    protected $table = 'record_transaction';
    protected $fillable = [
        'id' ,
        'uid',
        'ordersn',
        'money',
        'type',
        'time',
        'explain',
        'remark',
        'updated_at',
        'created_at',
    ];

    /**
     * 添加交易记录
     * @param array $order
     * @return bool|int
     */
    public function add_transaction($order = array())
    {
        if(empty($order)){
            return false;
        }
        $order['updated_at'] = date('Y-m-d H:i:s');
        $order['created_at'] = date('Y-m-d H:i:s');
        $rico = $this->insertGetId($order);
        if ($rico) {
            return $rico;
        } else {
            return false;
        }
    }

    /**
     * 统计数量
     * @param array $where
     * @return int
     */
    public function count_transaction($where = array())
    {
        return $this->where($where)->count();
    }

    /**
     * 获取
     * @param array $where 条件
     * @param bool $limit 查询多少条
     * @return array|bool
     */
    public function get_transaction($where = array(),$limit = null)
    {
        $table = $this->where($where)->orderBy('id','desc');
        if ($limit) {
            $table->limit($limit);
        }
        $rico = $table->get()->toArray();
        if ($rico) {
            return $rico;
        } else {
            return false;
        }
    }
}